/*
 *  Property Mapper, simple automatic GUI generator for JavaBeans using annotations.
 *  Copyright (C) 2008 Jon Lennersten, Johan Hedin
 *
 *  This file is part of Property Mapper.
 *
 *  Property Mapper is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Property Mapper is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Property Mapper.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.mawell.common.propertymapperswing.demo;

import com.mawell.common.propertymapperswing.PropertyView;
import com.mawell.common.propertymapperswing.PropertySettingController;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;

public class Main {

    public static void main(String args[]) {
        MainWindow f = new MainWindow();
        f.setSize(700, 500);

        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        }
        );

        f.setTitle("Propertymapper Demo");
        f.setVisible(true);
    }


    public static class MainWindow extends JFrame implements PropertySettingController {

        // A java.util.Set acts as the database.
        Set<ExampleEntity> entities = new HashSet<ExampleEntity>();

        public MainWindow() {
            // Populate "database"
            for(int i=0; i < 10; i++) {
                entities.add(new ExampleEntity(i+"", "Entity "+i));
            }

            PropertyObject po = new PropertyObject();
            PropertyView pView = new PropertyView(this, po, this, new ExampleTranslation(), "Header");
            this.getContentPane().add(pView);
        }

        public void propertySavePressed(Object theObject) {
            PropertyObject po = (PropertyObject)theObject;
            System.out.println("Object to be saved:\n" + po);

        }

        public void propertyDeletePressed() {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        public Map getEntityMap(String entity, String nameField) {
            // Using a database, nameField should refer to the column holding the name
            Map entMap = new HashMap();
            for(ExampleEntity e : entities) {
               entMap.put(e.getId(), e.getName());
            }
            return entMap;
        }
    }
}
